package com.fengchao.service;

import com.fengchao.config.Config;
import com.fengchao.dao.*;
import com.fengchao.model.*;
import com.fengchao.util.PushTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/11.
 */
@Service
@Transactional
public class TaskService {
    @Autowired
    OdrOrderMapper odrOrderMapper;
    @Autowired
    CfiConcertCrowdfundingPayBackMapper cfiConcertCrowdfundingPayBackMapper;
    @Autowired
    OdrOrderItemsMapper odrOrderItemsMapper;
    @Autowired
    CfiConcertCrowdfundingSingalMapper cfiConcertCrowdfundingSingalMapper;
    @Autowired
    SvyUserWishRequestMapper svyUserWishRequestMapper;
    @Autowired
    SvyUserWishRequestResultMapper svyUserWishRequestResultMapper;
    @Autowired
    UsrAppDeviceUserMapper usrAppDeviceUserMapper;
    @Autowired
    UsrPersonalMessageMapper usrPersonalMessageMapper;
    @Autowired
    PushTools pushTools;
    @Autowired
    PfmPerformanceTicketMapper pfmPerformanceTicketMapper;
    @Autowired
    OdrTicketInfoMapper odrTicketInfoMapper;


    private static final Logger logger = LoggerFactory.getLogger(TaskService.class);

    /**
     * 处理订单超时的
     */
    @Transactional
    public void OrderScan() {
        //获取超时的订单
        List<OdrOrder> orderList = odrOrderMapper.findOdrOrder(new Date());
        for (OdrOrder order : orderList) {
            logger.info("订单设置为过期= 订单id:{}", order.getId());
            if (Config.CROWDFUNDING.equals(order.getBusinessType())){
                //根据订单id查询订单的详细
                List<OdrOrderItems> odrOrderItemses = odrOrderItemsMapper.findByOrderId(order.getId());
                for (OdrOrderItems orderItems : odrOrderItemses) {
                    logger.info("订单的回收= 回报id:{}", orderItems.getId());
                    //根据订单详细的商品id查询回报信息
                    CfiConcertCrowdfundingPayBack cfiConcertCrowdfundingPayBack = cfiConcertCrowdfundingPayBackMapper.findByPayBackId(orderItems.getGoodsId());
                    Integer payBackLockedSum = cfiConcertCrowdfundingPayBack.getPayBackLockedSum() == null ? 0 : cfiConcertCrowdfundingPayBack.getPayBackLockedSum();
                    Integer soldSum = payBackLockedSum - orderItems.getGoodsTotalCount();
                    logger.info("回收库存=回报id为:{},回报锁定数量:{},回报待解锁数量为:{},回收之后的锁定数量为:{}", cfiConcertCrowdfundingPayBack.getId(), cfiConcertCrowdfundingPayBack.getPayBackLockedSum(), orderItems.getGoodsTotalCount(), soldSum);
                    //回收锁定的库存
                    cfiConcertCrowdfundingPayBackMapper.updatePayBackLockedSum(cfiConcertCrowdfundingPayBack.getId(), soldSum);
                }
            }else if(Config.PERFORMANCE.equals(order.getBusinessType())){
                //根据订单id查询订单的详细
                List<OdrOrderItems> odrOrderItemses = odrOrderItemsMapper.findByOrderId(order.getId());
                for (OdrOrderItems orderItems : odrOrderItemses) {
                    logger.info("订单的回收=票的id:{}", orderItems.getId());
                    //根据订单详细的商品id查询回报信息
                    PfmPerformanceTicket pfmPerformanceTicket= pfmPerformanceTicketMapper.findById(orderItems.getGoodsId());
                    Integer ticketLockSum = pfmPerformanceTicket.getPfmTicketLockSum()==null ? 0 : pfmPerformanceTicket.getPfmTicketLockSum();
                    Integer lockSum = ticketLockSum - orderItems.getGoodsTotalCount();
                    logger.info("回收库存=票id为:{},票的锁定数量:{},票的待解锁数量为:{},回收之后的锁定数量为:{}", pfmPerformanceTicket.getId(), ticketLockSum, orderItems.getGoodsTotalCount(), lockSum);
                    //回收锁定的库存
                    pfmPerformanceTicketMapper.updateTicketlockSum(pfmPerformanceTicket.getId(),lockSum);
                }
            }
            //将订单设置为失效状态
            odrOrderMapper.updateStatusInvalid(order.getId());
        }
    }

    /**
     * 更新有效票为失效
     */
    public void updateTicketExpired() {
        Integer count = odrTicketInfoMapper.updateTicketExpired();
        logger.info("更新行数 {}", count);
    }

    /**
     * 按时间 下架
     */
    @Deprecated
    public void updateCrowdfundingCancel() {
        Integer count = cfiConcertCrowdfundingSingalMapper.updateCrowdfundingCancel(new Date());
        if (count > 0) {
            logger.info("下架众筹数量：{}", count);
        }
    }


    /**
     * 统计心动艺人
     */
    @Deprecated
    public void statisticsUpdateWishReslt() {
        svyUserWishRequestResultMapper.deleteAll();
        List<Map<String, Object>> resultMap = svyUserWishRequestMapper.statistics();
        for (Map<String, Object> map : resultMap) {
            Integer wishArtistId = (Integer) map.get("wishArtistId");
            Integer wishCicyId = (Integer) map.get("wishCicyId");
            Long count = (Long) map.get("count");
            svyUserWishRequestResultMapper.saveSvyUserWishRequestResultMapper(wishArtistId, wishCicyId, count);
        }
    }

    /**
     * 推送系统消息
     */
    @Deprecated
    public void pushSysMessage() {
        //获取系统推送的消息
        List<UsrPersonalMessage> usrPersonalMessages = usrPersonalMessageMapper.findBySys();
        for (UsrPersonalMessage usrPersonalMessage : usrPersonalMessages) {
            //设置为已推送
            usrPersonalMessageMapper.updateIsPushByOrginMessageId(usrPersonalMessage.getOrginMessageId());
            //推送Jpush
            pushTools.Push(pushTools.buildPushObject_all_all_alert(usrPersonalMessage.getTitle()));
        }
    }


}
